import pkg from "./package";
import HappyPack from "happypack";
import webpack from "webpack";
import path from "path";
import os from "os";
import _ from "lodash/array";

const happyThreadPool = HappyPack.ThreadPool({
  size: os.cpus().length
});

export default {
  mode: "spa",
  /*
   ** Headers of the page
   */
  head: {
    title: "智慧校园",
    meta: [{
        charset: "utf-8"
      },
      {
        name: "viewport",
        content: "width=device-width, initial-scale=1"
      },
      {
        hid: "description",
        name: "description",
        content: process.env.npm_package_description || ""
      }
    ],
    link: [{
      rel: "icon",
      type: "image/x-icon",
      href: "/favicon.ico"
    }],
    script: [{
        src: "/js/vendor.dll.js"
      },
      {
        src: "/tinymce/tinymce-vue.js"
      },
      {
        src: "https://cdn.bootcss.com/echarts/4.1.0.rc2/echarts.min.js"
      }
    ]
  },
  /**
   * If you are using SPA mode, add an index / route to generate section of nuxt.config.js:
   */
  // generate: {
  //   routes: ["/"]
  // },
  // server: {
  //   port: 8000, // default: 3000
  //   host: "localhost" // default: localhost
  // },
  /*
   ** Customize the progress-bar color
   */
  loading: {
    color: "red"
  },

  /*
   ** Global CSS
   */
  css: [],

  /*
   ** Plugins to load before mounting the App
   */
  plugins: [{
      src: "~/plugins/ant",
      ssr: false
    },
    {
      src: "~/plugins/iview",
      ssr: false
    },
    {
      src: "~/plugins/axios",
      ssr: true
    },
    {
      src: "~/plugins/viser",
      ssr: false
    },
    {
      src: "~/plugins/Instance",
      ssr: true
    },
    {
      src: "~/plugins/gettext",
      ssr: true
    },
    {
      src: "~/plugins/directive",
      ssr: true
    },
    {
      src: "~/plugins/iconFont",
      ssr: true
    },
    {
      src: "~/plugins/business",
      ssr: false
    },
    {
      src: "~/plugins/publicComponent",
      ssr: false
    },
    {
      src: "~/plugins/permission",
      ssr: false
    },
    {
      src: "~/plugins/animated",
      ssr: false
    },
    {
      src: "~/plugins/vcolorpicker",
      ssr: false
    }
  ],

  /*
   ** Nuxt.js modules
   */
  modules: [
    [
      "nuxt-vuex-localstorage",
      {
        // mode: "manual",
        localStorage: ["PermisionsStorage"]
      }
    ],
    // 配置选项(缓存组件 name属性必须唯一)
    // [
    //   "@nuxtjs/component-cache",
    //   {
    //     max: 10000,
    //     maxAge: 1000 * 60 * 60
    //   }
    // ],
    // Doc: https://axios.nuxtjs.org/usage
    "@nuxtjs/axios",
    // With options
    [
      "cookie-universal-nuxt",
      {
        alias: "cookies"
      }
    ],
    [
      "@nuxtjs/router",
      {
        keepDefaultRouter: true,
        path: "./router/",
        fileName: "index.js"
      }
    ]
  ],
  /*
   ** Axios module configuration
   */
  axios: {
    // See https://github.com/nuxt-community/axios-module#options
    proxy: true
  },
  proxy: {
    "/new/": {
      target: "http://easy.dev.orhonedu.com/mock/5d0c3bb8a1e1d5328d25c4c5/Systemctl/",
      pathRewrite: {
        "^/new/": ""
      }
    },
    "/api/": {
      target: "http://api.dev.orhonedu.com/",
      pathRewrite: {
        "^/api/": ""
      }
    },
  },
  /*
   ** Build configuration
   */
  build: {
    /*
     ** You can extend webpack config here
     */
    extend(config, ctx) {
      const sassResourcesLoader = {
        loader: "sass-resources-loader",
        options: {
          resources: ["assets/style/theme.less", "assets/style/mixins.less"]
        }
      };
      config.resolve.alias["&"] = __dirname + "/components/";
      config.resolve.alias["$project"] = __dirname + "/utils/project.json";
      config.resolve.alias["$m"] = __dirname + "/view/";
      config.externals = {
        editor: "Editor",
        echarts: "echarts"
      };
      config.module.rules.forEach(rule => {
        if (rule.test.toString() === "/\\.vue$/") {
          rule.options.loaders.less.push(sassResourcesLoader);
        }
        if (rule.test.toString() === "/\\.less$/") {
          rule.use.push(sassResourcesLoader);
        }
      });
    },
    plugins: [
      // new webpack.ProvidePlugin({
      //   _: "lodash"
      // })
      new HappyPack({
        //用id来标识 happypack处理那里类文件
        id: "happyBabel",
        //如何处理  用法和loader 的配置一样
        loaders: [{
          loader: "babel-loader?cacheDirectory=true"
        }],
        //共享进程池
        threadPool: happyThreadPool,
        //允许 HappyPack 输出日志
        verbose: true
      }),
      new webpack.DllReferencePlugin({
        manifest: require(path.join(
          __dirname,
          "./",
          "static/js",
          "manifest.json"
        ))
      })
    ],
    watchOptions: {
      ignored: /node_modules/, //忽略不用监听变更的目录
      aggregateTimeout: 500, //防止重复保存频繁重新编译,500毫米内重复保存不打包
      poll: 1000 //每秒询问的文件变更的次数
    }
  },
  router: {
    // extendRoutes(routes, resolve) {
    //   //需要排出的name列表
    //   const list = [
    //     {
    //       name: "functionModule"
    //     }
    //     // {name: 'login'},
    //   ];
    //   const value = _.pullAllBy(routes, list, "name");
    //   return value;
    // }
  }
};